#include "w.h"
#include "oledfont.h"  	 
#include "stm32f1xx_hal.h"	
#include "bmp.h"
#include <stdio.h>

#define OLED_ADDRESS	0x78 //通过调整0R电阻,屏可以选择0x78  0x7A两个地址   默认为0x78
#define OLED_TIMEOUT    200  //200毫秒超时等待
#define OLED_CMDmode    0x00 //写命令模式
#define OLED_DATAmode   0x40 //写数据模式

extern I2C_HandleTypeDef hi2c1;
extern uint32_t CapVal1,CapVal2;
extern uint8_t OverPeriod,FinishFlag;

uint8_t OLED[8][128]={0};
uint8_t CMD_Data[]={
0xAE, 0x00, 0x10, 0x40, 0xB0, 0x81, 0xFF, 0xA1, 0xA6, 0xA8, 0x3F,
					
0xC8, 0xD3, 0x00, 0xD5, 0x80, 0xD8, 0x05, 0xD9, 0xF1, 0xDA, 0x12,
					
0xDB, 0x30, 0x8D, 0x14, 0xAF,0x20,0x00};
uint8_t CMD_Sleep[3]={0x8D,0x10,0xAE};
uint8_t CMD_WakeUp[3]={0x8D,0x14,0xAF};


void OLED_Write(uint8_t* content,uint8_t OLED_DATAorCMDmode)
{
	HAL_I2C_Mem_Write(&hi2c1,OLED_ADDRESS,OLED_DATAorCMDmode,I2C_MEMADD_SIZE_8BIT,content,strlen((char*)content),OLED_TIMEOUT);
}


//更新oled数组数据至oled
void OLED_Refreash(void)
{
	//HAL_I2C_Mem_Write(&hi2c1 ,0x78,0x40,I2C_MEMADD_SIZE_8BIT,&data,1,0xff);
	HAL_I2C_Mem_Write(&hi2c1,OLED_ADDRESS,OLED_DATAmode,I2C_MEMADD_SIZE_8BIT,OLED[0],1024,OLED_TIMEOUT);
}
//初始化oled屏幕
void OLED_Init(void)
{ 	
	HAL_Delay(200);//这里的延时很重要
	HAL_I2C_Mem_Write(&hi2c1,OLED_ADDRESS,OLED_CMDmode,I2C_MEMADD_SIZE_8BIT,CMD_Data,29,OLED_TIMEOUT);
}
//清屏
void OLED_Clean(void)
{
	uint8_t i,n;		    
	for(i=0;i<8;i++)  
	{  
		for(n=0;n<128;n++)
		OLED[i][n]=0;
	}
	OLED_Refreash();
}

void OLED_ShowBMP(void)
{
	HAL_I2C_Mem_Write(&hi2c1,OLED_ADDRESS,OLED_DATAmode,I2C_MEMADD_SIZE_8BIT,BMP1,1024,OLED_TIMEOUT);
}

void OLED_Sleep(void)
{
	HAL_I2C_Mem_Write(&hi2c1,OLED_ADDRESS,OLED_CMDmode,I2C_MEMADD_SIZE_8BIT,CMD_Sleep,3,OLED_TIMEOUT);
}
	
void OLED_WakeUp(void)
{
	HAL_I2C_Mem_Write(&hi2c1,OLED_ADDRESS,OLED_CMDmode,I2C_MEMADD_SIZE_8BIT,CMD_WakeUp,3,OLED_TIMEOUT);
}
	
unsigned int oled_pow(uint8_t m,uint8_t n)
{
	unsigned int result=1;	 
	while(n--)result*=m;    
	return result;
}
//显示2个数字
//x,y :起点坐标	 
//len :数字的位数
//size:字体大小
//mode:模式	0,填充模式;1,叠加模式
//num:数值(0~4294967295);	 		  
void OLED_ShowNum(uint8_t x,uint8_t y,unsigned int num,uint8_t len,uint8_t size12or16)
{
	uint8_t t,temp;
	uint8_t enshow=0;
	
	for(t=0;t<len;t++)
	{
		temp=(num/oled_pow(10,len-t-1))%10;
		if(enshow==0&&t<(len-1))
		{
			if(temp==0)
			{
				OLED_ShowChar(x+(size12or16/2)*t,y,' ',size12or16,0);
				continue;
			}else enshow=1; 
		 	 
		}
	 	OLED_ShowChar(x+(size12or16/2)*t,y,temp+'0',size12or16,0); 
	}
} 
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//mode:0,反白显示;1,正常显示				 
//size:选择字体 16/12 
void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size12or16,uint8_t Is_Reverse)
{      	
	unsigned char c=0,i=0;	
	c=chr-' ';//得到偏移后的值			
	if(x>128-1){x=0;y=y+2;}
	if(size12or16 ==16)
	{
		if(Is_Reverse==0)
		{
			for(i=0;i<8;i++)
			{
				OLED[y][x+i] = F8X16[c*16+i];		
				OLED[y+1][x+i] = F8X16[c*16+i+8];	
			}				
		}
		else
		{
			OLED[y][x+i] = ~F8X16[c*16+i];		
			OLED[y+1][x+i] = ~F8X16[c*16+i+8];				
		}
	}
	else 
	{	
		if(Is_Reverse==0)
		{
			for(i=0;i<6;i++)
			OLED[y][x+i] = F6x8[c][i];
		}
		else
		{
			for(i=0;i<6;i++)
			OLED[y][x+i] = ~F6x8[c][i];
		}
	}
}
 
//显示一个字符号串
void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t size12or16)
{
	unsigned char j=0;
	while (chr[j]!='\0')
	{
		OLED_ShowChar(x,y,chr[j],size12or16,0);
		x+=8;
		if(x>120)
		{
			x=0;
			y+=2;
		}
		j++;
	}
}

//显示汉字
//hzk 用取模软件得出的数组
void OLED_ShowCHinese(uint8_t x,uint8_t y,uint8_t no)
{      			    
	uint8_t t;

    for(t=0;t<16;t++)
	{
		OLED[y][x+t]=Hzk[2*no][t];
    }		
    for(t=0;t<16;t++)
	{	
		OLED[y+1][x+t]=Hzk[2*no+1][t];
    }					
}

void ECHO(char* result)
{
	uint32_t diff=0,difcm=0;
	char difs[11];
	HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_SET);
	HAL_Delay(1);
	HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,GPIO_PIN_RESET);
	for(int num=0;num<20;num++)
	{
		if(FinishFlag==1)
		{
			
			diff=85*(CapVal2-CapVal1+OverPeriod*10000);
			difcm=diff/10000;
			OverPeriod=0;
			CapVal2=0;
			CapVal1=0;
			FinishFlag=0;
			sprintf(result,"%u.",difcm);
			sprintf(difs,"%u",diff-(difcm*10000));
			strcat(result,difs);
			//sprintf(result,"%u",diff);
			return;
		}
		else
		{
			HAL_Delay(50);
		}
	}
	return;
}

